﻿/*
6
abc def
efc
dbc
ebc
dec
dfc
yhn
*/
// N个字符串
int N = int.Parse(Console.ReadLine());
string[] pointStr = Console.ReadLine().Split(" ");
(string beginStr, string endStr) = (pointStr[0], pointStr[1]);

// 邻接表 -- 存储图
Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>();
graph[beginStr] = new List<string>();
for (int i = 0; i < N; i++)
{
    string temp = Console.ReadLine();
    graph[temp] = new List<string>();
}
graph[endStr] = new List<string>();

// 判断两个字符串之间是否可以转换
bool isNear(string str1, string str2)
{
    int different = 0;
    for (int i = 0; i < str1.Length; i++)
    {
        if (str1[i] != str2[i]) different++;
    }

    return different <= 1;
}

// 添加边
List<string> keyList = graph.Keys.ToList();
for (int i = 0; i < keyList.Count; i++)
{
    for (int j = i + 1; j < keyList.Count; j++)
    {
        bool flag = isNear(keyList[i], keyList[j]);
        if (flag)
        {
            graph[keyList[i]].Add(keyList[j]);
            graph[keyList[j]].Add(keyList[i]);
        }
    }
}

// 广度优先搜索，求最短距离
int BFS(Dictionary<string, List<string>> graph, string beginStr, string endStr)
{
    Queue<string> queue = new Queue<string>();
    Dictionary<string, int> distance = new Dictionary<string, int>(); // 存储到各点的最短距离
    queue.Enqueue(beginStr);
    distance[beginStr] = 1;
    while (queue.Count > 0)
    {
        string tempStr = queue.Dequeue();
        
        if (tempStr.Equals(endStr))
        {
            break;
        }
        
        foreach (string str in graph[tempStr])
        {
            if (!distance.ContainsKey(str))
            {
                distance[str] = distance[tempStr] + 1;
            }
            queue.Enqueue(str);
        }
    }
    return distance.ContainsKey(endStr) ? distance[endStr] : 0;
}
int res = BFS(graph, beginStr, endStr);
Console.WriteLine(res);